<?php
declare (strict_types=1);

namespace app\controller;

use app\common\UsersService;
use app\model\ActivityComment;
use app\model\ForumComment;
use app\model\FriendsComment;
use app\model\NewsComment;
use app\model\QuestionAnswer;
use app\model\QuestionAnswerComment;
use app\model\RoomComment;
use app\model\Score;
use app\model\SecGoodsComment;
use app\model\StoreAssessComment;
use app\model\TalentComment;
use app\model\Task;
use app\model\Users;
use app\model\UsersLike;
use app\model\UsersOnline;
use app\model\UsersSign;
use app\Request;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;
use think\Exception;
use think\response\Json;

class TaskController
{
	protected array $middleware = ['Login' => [
		'except' => ['index', 'online', 'share'],
	]];
	
	/**
	 * 分享统计
	 * @param Request $request
	 */
	public function share(Request $request)
	{
		$row = UsersOnline::where(['users_id' => $request->users_id])->whereTime('createtime', 'today')->find();
		if (!$row) {
			UsersOnline::create(['users_id' => $request->users_id, 'share' => 1]);
		} else {
			$row->share += 1;
			$row->save();
		}
		writeJson();
	}
	
	
	/**
	 * 任务详情
	 * @param Request $request
	 */
	public function read(Request $request)
	{
		$online = UsersOnline::where(['users_id' => $request->users_id])->whereTime('createtime', 'today')->find();
		if (!$online) {
			$online_time = 0;
			$receive_brower = '';
			$receive_daily = '';
			$share = 0;
		} else {
			$online_time = $online->online_time;
			$receive_brower = $online->receive_brower;
			$receive_daily = $online->receive_daily;
			$share = $online->share;
		}
		$minute = intval($online_time / 1000 / 60);
		$brower = Task::where(['type' => 1])->order(['minute' => 'asc'])->filter(function ($data) use ($online_time, $receive_brower, $minute) {
			//receive  0=不可领取 1=未领取 2=已领取
			if ($data->minute <= $minute) {
				//可领取或已领取
				if (empty($receive_brower)) {
					//没有领取记录 没有领取
					$data->receive = 1;
				} else {
					//有领取记录 判断是否领取
					$data->receive = in_array($data->id, $receive_brower) ? 2 : 1;
				}
			} else {
				//不可领取
				$data->receive = 0;
			}
		})->select();
		
		$daily = Task::where(['type' => 2])->filter(function ($data) use ($request, $receive_daily, $share) {
			switch ($data->id) {
				case 7:
					$count = QuestionAnswer::where(['users_id' => $request->users_id])->whereTime('createtime', 'today')->limit($data->num)->select()->count();
					break;
				case 8:
					$count = UsersLike::where(['users_id' => $request->users_id])->whereTime('createtime', 'today')->limit($data->num)->select()->count();
					break;
				case 9:
					$activity_comment = ActivityComment::where(['users_id' => $request->users_id])->whereTime('createtime', 'today')->count();
					$forum_comment = ForumComment::where(['users_id' => $request->users_id])->whereTime('createtime', 'today')->count();
					$friends_comment = FriendsComment::where(['users_id' => $request->users_id])->whereTime('createtime', 'today')->count();
					$new_comment = NewsComment::where(['users_id' => $request->users_id])->whereTime('createtime', 'today')->count();
					$question_comment = QuestionAnswerComment::where(['users_id' => $request->users_id])->whereTime('createtime', 'today')->count();
					$room_comment = RoomComment::where(['users_id' => $request->users_id])->whereTime('createtime', 'today')->count();
					$sec_goods_comment = SecGoodsComment::where(['users_id' => $request->users_id])->whereTime('createtime', 'today')->count();
					$store_assess_comment = StoreAssessComment::where(['users_id' => $request->users_id])->whereTime('createtime', 'today')->count();
					$talent_comment = TalentComment::where(['users_id' => $request->users_id])->whereTime('createtime', 'today')->count();
					$count = $activity_comment + $forum_comment + $friends_comment + $new_comment + $question_comment + $room_comment + $sec_goods_comment + $store_assess_comment + $talent_comment;
					break;
				case 10:
					$count = $share;
					break;
				case 11:
					$count = QuestionAnswer::where(['users_id' => $request->users_id, 'reght' => 1])->whereTime('createtime', 'today')->limit($data->num)->select()->count();
					break;
			}
			$data->count = $count;
			if ($count >= $data->num) {
				
				//可领取或已领取
				if (empty($receive_daily)) {
					//没有领取记录 没有领取
					$data->receive = 1;
				} else {
					//有领取记录 判断是否领取
					$data->receive = in_array($data->id, $receive_daily) ? 2 : 1;
				}
			} else {
				//不可领取
				$data->receive = 0;
			}
		})->select();
		writeJson(['online_time' => $minute, 'brower' => $brower, 'daily' => $daily]);
	}
	
	/**
	 * 完成任务 领取积分
	 */
	function receive(Request $request)
	{
		$id = $request->post('id/d');
		$row = Task::findOrFail($id);
		$online = UsersOnline::where(['users_id' => $request->users_id])->whereTime('createtime', 'today')->find();
		if (!$online) {
			$online_time = 0;
			$receive_brower = '';
			$receive_daily = '';
			$share = 0;
		} else {
			$online_time = $online->online_time;
			$receive_brower = $online->receive_brower;
			$receive_daily = $online->receive_daily;
			$share = $online->share;
		}
		$minute = intval($online_time / 1000 / 60);
		if ($row->type == 1) {
			if ($row->minute > $minute) {
				writeJson(0, '在线时间不足');
			}
			if ($receive_brower && in_array($id, $receive_brower)) {
				writeJson(0, '不能重复领取');
			}
			$receive_brower = $online->receive_brower;
			$receive_brower[] = $id;
			$online->receive_brower = $receive_brower;
			$mark = '完成浏览任务';
		} else {
			switch ($id) {
				case 7:
					$count = QuestionAnswer::where(['users_id' => $request->users_id])->whereTime('createtime', 'today')->limit($row->num)->select()->count();
					break;
				case 8:
					$count = UsersLike::where(['users_id' => $request->users_id])->whereTime('createtime', 'today')->limit($row->num)->select()->count();
					break;
				case 9:
					$activity_comment = ActivityComment::where(['users_id' => $request->users_id])->whereTime('createtime', 'today')->count();
					$forum_comment = ForumComment::where(['users_id' => $request->users_id])->whereTime('createtime', 'today')->count();
					$friends_comment = FriendsComment::where(['users_id' => $request->users_id])->whereTime('createtime', 'today')->count();
					$new_comment = NewsComment::where(['users_id' => $request->users_id])->whereTime('createtime', 'today')->count();
					$question_comment = QuestionAnswerComment::where(['users_id' => $request->users_id])->whereTime('createtime', 'today')->count();
					$room_comment = RoomComment::where(['users_id' => $request->users_id])->whereTime('createtime', 'today')->count();
					$sec_goods_comment = SecGoodsComment::where(['users_id' => $request->users_id])->whereTime('createtime', 'today')->count();
					$store_assess_comment = StoreAssessComment::where(['users_id' => $request->users_id])->whereTime('createtime', 'today')->count();
					$talent_comment = TalentComment::where(['users_id' => $request->users_id])->whereTime('createtime', 'today')->count();
					$count = $activity_comment + $forum_comment + $friends_comment + $new_comment + $question_comment + $room_comment + $sec_goods_comment + $store_assess_comment + $talent_comment;
					break;
				case 10:
					$count = $share;
					break;
				case 11:
					$count = QuestionAnswer::where(['users_id' => $request->users_id, 'reght' => 1])->whereTime('createtime', 'today')->limit($row->num)->select()->count();
					break;
				default:
					writeJson(0, '领取失败');
			}
			
			if ($count < $row->num) {
				writeJson(0, '未完成指定次数');
			}
			
			if ($receive_daily && in_array($row->id, $receive_daily)) {
				writeJson(0, '不能重复领取');
			}
			
			$receive_daily = $online->receive_daily;
			$receive_daily[] = $id;
			$online->receive_daily = $receive_daily;
			$mark = '完成每日任务';
		}
		$online->save();
		try {
			UsersService::changeScore($row->score, $request->users_id, $mark);
		} catch (Exception $e) {
			writeJson(0, $e->getMessage());
		}
		writeJson();
	}
	
	/**
	 * 签到
	 * @param Request $request
	 */
	function sign(Request $request)
	{
		$today = UsersSign::where('users_id', $request->users_id)->whereTime('createtime', 'today')->find();
		if ($today) {
			writeJson(0, '今日已签到');
		}
		$yesterday = UsersSign::where('users_id', $request->users_id)->whereTime('createtime', 'yesterday')->find();
		if ($yesterday && $yesterday->max_sign != 7) {
			$score = Score::find($yesterday->max_sign + 1);
			$data = ['users_id' => $request->users_id, 'max_sign' => $yesterday->max_sign + 1, 'score' => $score->score];
			$mark = '连续签到' . ($yesterday->max_sign + 1) . '天';
		} else {
			$score = Score::find(1);
			$data = ['users_id' => $request->users_id, 'max_sign' => 1, 'score' => $score->score];
			$mark = '连续签到1天';
		}
		UsersSign::create($data);
		try {
			UsersService::changeScore($score->score, $request->users_id, $mark);
		} catch (Exception $e) {
			writeJson(0, $e->getMessage());
		}
		writeJson();
	}
	
	/**
	 * 签到提醒开关
	 * @param Request $request
	 */
	function sign_notice(Request $request)
	{
		$users = Users::find($request->users_id);
		if ($users->sign_notice == 0) {
			$users->sign_notice = 1;
		} else {
			$users->sign_notice = 0;
		}
		$users->save();
		
		writeJson(1, 'ok', $users->sign_notice);
	}
	
	/**
	 * 签到详情
	 * @param Request $request
	 */
	function sign_detail(Request $request)
	{
		$yesterday = UsersSign::where('users_id', $request->users_id)->whereTime('createtime', 'yesterday')->find();
		$today = UsersSign::where('users_id', $request->users_id)->whereTime('createtime', 'today')->find();
		
		$rows = Score::order(['id' => 'asc'])->filter(function ($data) {
			$data->sign = false;
		})->select();
		if ($yesterday && $yesterday->max_sign != 7) {
			$arr = [];
			if ($today) {
				$max_sign = $today->max_sign;
			} else {
				$max_sign = $yesterday->max_sign;
			}
			for ($i = $max_sign; $i >= 1; $i--) {
				$arr[] = $i;
			}
			$rows->filter(function ($data) use ($arr) {
				if (in_array($data->id, $arr)) {
					$data->sign = true;
				}
			});
		} else {
			if ($today) {
				$rows->filter(function ($data) {
					if ($data->id == 1) {
						$data->sign = true;
					}
				});
			}
		}
		$sign_notice = Users::getFieldById($request->users_id, 'sign_notice');
		if ($today) {
			$max_sign = $today->max_sign;
		} else {
			if ($yesterday) {
				$max_sign = $yesterday->max_sign;
			} else {
				$max_sign = 0;
			}
		}
		
		$max_sign = '连续签到' . $max_sign . '天';
		writeJson(['list' => $rows, 'sign' => (bool)$today, 'sign_notice' => $sign_notice, 'max_sign' => $max_sign]);
	}
	
	/**
	 * 记录在线时长
	 * @param Request $request
	 * @return Json
	 */
	function online(Request $request)
	{
		$token = $request->header('Authorization') ?? 1;
		$userInfo = check_token($token);
		if ($userInfo['code'] != 200) {
			writeJson();
		}
		$request->users_id = $userInfo['data']['uid'];
		$online_time = $request->post('online_time');
		$row = UsersOnline::where(['users_id' => $request->users_id])->whereTime('createtime', 'today')->find();
		if (!$row) {
			UsersOnline::create(['users_id' => $request->users_id, 'online_time' => $online_time]);
		} else {
			$row->online_time += $online_time;
			$row->save();
		}
		writeJson();
	}
	
}
